# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os,time
#制定数据的输出

class DoubanmoviePipeline(object):
    #构造方法
    def __init__(self):
        #创建文件夹
        self.folder_name = 'output'
        if not os.path.exists(self.folder_name):
            os.mkdir(self.folder_name)
            pass
        #获取时间
        current_time = time.strftime('%Y-%m-%d-%H:%M',time.localtime())
        #创建文件  output/doubanmovie_top_2018-7-4-10-02.txt.lstrip("演:")
        self.filename = self.folder_name+os.sep+'doubanmovie_top_'+current_time+'.txt'



    def process_item(self, item, spider):
        #将数据存入文件

        with open(self.filename,'a',encoding = 'utf-8') as fd:
            fd.write('排名：%s'%item['Mov_Rank'][0]+'     ')
            fd.write('名称：%s'%item['Mov_Name'][0]+'     ')
            fd.write('海报：%s' % item['Mov_pic'][0] + '     ')
            fd.write('评分：%s' % item['Mov_grade'][0] + '     ')
            fd.write('描述：%s' % item['Mov_discribe'][0]+ '     ')
            st = item['Mov_abstract'][0].lstrip().split('主')
            fd.write('导演：%s' %st[0].lstrip('导演:')+ '     ')
            fd.write('主演：%s' %st[1].lstrip('演:')+ '     ')
            fd.write('上映时间：%s' % item['Mov_abstract'][1].lstrip().split('/')[0]+ '     ')
            fd.write('国家：%s' % item['Mov_abstract'][1].lstrip().split('/')[1] + '     ')
            fd.write('类型：%s' % item['Mov_abstract'][1].lstrip().split('/')[2] + '     ')
            fd.write('\n')

            pass
        return item
